<?php
class EduUtil
{
    private function __construct()
    {
    }
    
	/**
     * 由id获取学校名称
     * @param <int> $id
     * @return <string>
    */
    public static function getLevelName($type){
        if ($type == 1)
        {
            return '小学';
        }elseif ($type == 2)
        {
            return '初中';
        }elseif ($type == 3)
        {
            return '高中';
        }elseif ($type == 4)
        {
            return '大学';
        }elseif ($type == 5)
        {
            return '硕士';
        }elseif ($type == 6)
        {
            return '博士';
        }elseif ($type == 7)
        {
            return '工作';
        }else
        {
            return "";
        }
    }
   /**
     * 由id获取学校名称
     * @param <int> $id
     * @return <string>
    */
	public static function getSchollNameById($id){
		if (empty($id))
        {
            return "id不能为空";
        }
        $IndexId2Name = $id % 500;
        $filePath = MB_COMM_DIR."/edu/id2name/".$IndexId2Name.".php";
		if (!file_exists($filePath))
        {
            return "文件不存在";
        }
        @include_once MB_COMM_DIR."/edu/id2name/".$IndexId2Name.".php";
        return $eduIdMap[$id];
	}

    /**
     * 由学校名称获取id
     * @param <string> $name
     * @return <int>
     */
    public static function getSchoolIdByName($name)
    {
        if (empty($name))
        {
            return 0;
        }
        $IndexName2Id = sprintf("%02x",ord($name[0]));
        if (ord($name[0]) > 0xe0)
        {
            $IndexName2Id .= sprintf("%02x",ord(($name[1])));
        }
        $filePath = MB_COMM_DIR."/edu/name2id/".$IndexName2Id.".php";
        if (!file_exists($filePath))
        {
            return 0;
        }

        require MB_COMM_DIR."/edu/name2id/".$IndexName2Id.".php";
        $result = explode(",", $eduMap[$name]);
        return $result[0];
    }

    /**
     * 由院系名称获取id
     * @param <int> $schoolId
     * @param <string> $name 院系名称
     * @return <int>
     */
    public static function getDepartmentIdByName($schoolId, $name)
    {
        if (empty($schoolId))
        {
            return array();
        }
        $dirName = $schoolId % 200;
        $filePath = MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (!file_exists($filePath))
        {
            return array();
        }
        require MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (empty($depMap))
        {
            return array();
        }
        $depMap = $depMap[$schoolId];
        foreach($depMap as $key => $value)
        {
            if ($value == $name)
            {
                return $key;
            }
        }
        return 0;
    }

    /**
     * 由学校id获取院系列表
     * @param <int> $schoolId
     * @return <array of string>
     */
    public static function getDepartmentListBySchoolId($schoolId)
    {
        if (empty($schoolId))
        {
            return array();
        }
        $dirName = $schoolId % 200;
        $filePath = MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (!file_exists($filePath))
        {
            return array();
        }
        require MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (empty($depMap))
        {
            return array();
        }
        return $depMap[$schoolId];
    }

    /**
     * 由学校院系ID获取院系名称
     * @param <int> $schoolId
     * @param <int> $id
     * @return <string>
     */
    public static function getDepartmentNameById($schoolId, $id)
    {
        if (empty($schoolId))
        {
            return "";
        }
        $dirName = $schoolId % 200;
        $filePath = MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (!file_exists($filePath))
        {
            return "";
        }
        require MB_COMM_DIR."/edu/scholldepartment/".$dirName.".php";
        if (empty($depMap))
        {
            return "";
        }
        $depMap = $depMap[$schoolId];
        foreach($depMap as $key => $value)
        {
            if ($key == $id)
            {
                return $value;
            }
        }
        return "";
    }

    /**
     * 由地区获取学校列表
     * @param <string> $region
     * @param <int> $type
     * @return <array of string>
     */
    public static function getSchoolListByRegion($region, $type, $letter='')
    {
    	//参数转换
        if ($type == 4 || $type == 5 || $type == 6)
        {//网页中的大学/硕士/博士
            $type = 3;//数据库中的大学
        }elseif ($type  == 2 || $type  == 3)
        {//网页中的初中/高中
            $type = 2;//数据库中的中学
        }else
        {
            $type = 1;
        }
        if ($type == 2 && (mb_substr_count($region, "_")==2))
        {
            $pos = mb_strrpos($region, "_");

            $district = mb_substr($region, $pos + 1);
            $region = mb_substr($region, 0, $pos);
        }
        $filename = urlencode($region).".list";

        if ($type == 2)//中学
        {
            $filePath = MB_COMM_DIR."/edu/schoollist/".$filename;
        }elseif ($type == 3)//大学
        {
            $filePath = MB_COMM_DIR."/edu/schoollist/campus/".$filename;
        }elseif ($type == 1)//小学
        {
            $filePath = MB_COMM_DIR."/edu/prischoollist/".$filename;
        }else
        {
            return array();
        }
		
        if (!file_exists($filePath))
        {
            return array();
        }

        if ($type == 2)
        {
            require $filePath;
            $data = $schoolist[$district];
        }else
        {
            $data = file_get_contents($filePath);
        }
        $result = array();
        if (!empty($data))
        {
            $dataArray = explode(",",$data);
            foreach ($dataArray as $value)
            {
            	if($value){
            		$stName = explode(":", $value);
            		$tempArr = array(
            			"name" => $stName[0],
            			"id" => $stName[1],
            			"letter"=>$stName[2]
            		);
            		if($letter){
            			$letter = trim(strtoupper($letter));
            			if($letter == $stName[2]){
            				$result[]=$tempArr;
            				continue;
            			}
            		}else{
            			$result[]=$tempArr;
            		}
            	}
            }
        }
        return $result;
    }
}
?>